<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Article;
use App\Entity;
use Illuminate\Support\Facades\Cache;

class ArticleController extends Controller
{
	
		
    
	public function index() {
	// Cache::add('key',"value",60);
	 //$value = Cache::get('key');
	 //dd($value);	
  	// Cache::store('file')->put('bar','baz',20);
	//$value = Cache::store("redis")->get('bar');
	// dd($value);

        $res = ["stat" => 0, "msg" => "error"];
        $t5 = microtime(true);    
        $articles = Article::where('id', '>', 0)->where('id', '<', 16)->get();
        $data  = [];
        foreach ($articles as $item) {
		$data[]  = [
			"id"=>$item->id,
			"user_id"=>$item->user_id,
			"title"=>$item->title,"craete_time"=>(string)$item->created_at];
	}
	if(!empty($data)){
		$res['stat'] =1;
		$res['msg']  = "succ";
	}
        $t6 =  microtime(true);
	$tt = (($t6 - $t5) * 1000) . 'ms';
        $res["runtime"] = $tt;
        $res["data"] = $data;
        echo json_encode($res);
    }

    /*
    public function show(){
	   $t5 = microtime(true); 	
	   $entitys = Entity::where('create_time','>','2016-06-01')->skip(0)->take(5)->get();
    	    foreach($entitys as $item){
	    	$data[] = array(
			"entity_id"=>$item->entity_id,
			"entity_type"=>$item->entity_type,
			"create_time" =>$item->create_time

		);
	    }	
	   $t6 =  microtime(true);
	   $tt = (($t6 - $t5) * 1000) . 'ms';
           $data["runtime"] = $tt;
	   echo json_encode($data);
    }
     */
    
    public function show($id){
        return view('article/show')->withArticle(Article::with('hasManyComments')->find($id));
    }
}



